package Config;

import controller.intercepter.FileInterceptor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.io.FileSystemResource;
import org.springframework.web.multipart.commons.CommonsMultipartResolver;
import org.springframework.web.servlet.config.annotation.EnableWebMvc;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

import java.io.IOException;

@Configuration
@ComponentScan("controller")
@EnableWebMvc
public class SpringMvcConfig implements WebMvcConfigurer {

    @Autowired
    private FileInterceptor intercepter;

    @Override
    public void addInterceptors(InterceptorRegistry registry) {
        registry.addInterceptor(intercepter).addPathPatterns("/files/download");
    }

    @Bean
    public CommonsMultipartResolver multipartResolver() throws IOException {
        CommonsMultipartResolver resolver = new CommonsMultipartResolver();

        // 设置最大上传文件大小 (以字节为单位)，这里设置为200MB
        resolver.setMaxUploadSize(200 * 1024 * 1024);

        // 设置最大内存缓存大小 (以字节为单位)，这里设置为30MB
        resolver.setMaxInMemorySize(30 * 1024 * 1024);

        // 设置上传文件临时目录，如果不设置，默认使用系统临时目录
         resolver.setUploadTempDir(new FileSystemResource("F://share"));

        return resolver;
    }



}
